package com.fengye.algorithms.leecode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author fengye
 * @Date 2023/4/24 11:12
 **/
public class IntervalListIntersections986 {

    public static void main(String[] args) {
        new IntervalListIntersections986().intervalIntersection(new int[][]{{0,2},{5,10},{13,23},{24,25}},
                new int[][]{{1,5},{8,12},{15,24},{25,26}});
    }

    public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
        List<int[]> ans = new ArrayList<>();

        int f = 0, s = 0;

        while (f < firstList.length && s < secondList.length) {
            int lower = Math.max(firstList[f][0], secondList[s][0]);
            int high = Math.min(firstList[f][1], secondList[s][1]);
            // 符合条件
            if(lower <= high) {
                ans.add(new int[] {lower,high});
            }
            // 小的向前
            if(firstList[f][1] < secondList[s][1]) {
                f++;
            } else {
                s++;
            }
        }

        return ans.toArray(new int[ans.size()][]);
    }
}
